// var httpUrl = "https://mp.ameimeika.com/";//测试服
var httpUrl = "https://app.ameimeika.com/";//正式服
var u = navigator.userAgent,
    app = navigator.appVersion;
//执行loading
//获取url
var strUrl = window.location.href;//当前地址
var urlstr = strUrl.split("?")[(strUrl.split('?')).length - 1];

var token;//定义token
var model;//定义三端类型
var share_id;//定义分享id
if (urlstr) {
    var url_data = {};
    urlstr.split("&").forEach(function (item) {
        url_data[item.split("=")[0]] = item.split("=")[1];
    });
    sessionStorage.setItem("token", url_data.token || url_data.user_token)
    token = sessionStorage.getItem("token")
    model = url_data.model
    share_id = url_data.share_id || ""
}
if (/MicroMessenger/i.test(u)) {
    // 微信浏览器
    wx.miniProgram.getEnv(function (res) {
        if (res.miniprogram) {
            sessionStorage.setItem("broswer", 'mp')
        }
    });
} else {
    // app环境
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g
    var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端 
    if (isAndroid) {
        sessionStorage.setItem("broswer", 'android')
    }
    if (isIOS) {
        sessionStorage.setItem("broswer", 'ios')
    }
}


//手机号验证
var phoneFlag1, phoneFlag2;
function phoneReg(e, n) {
    var phone = $(e).val();
    var tel = /^1([38]\d|5[0-35-9]|7[3678])\d{8}$/;
    if (tel.test(phone)) {
        if (n == 1) {
            phoneFlag1 = true;
        } else {
            $(".divs").hide()
            phoneFlag2 = true;
        }
    } else {
        if (n == 1) {
            phoneFlag1 = false;
        } else {
            $(".divs").hide()
            phoneFlag2 = false;
        }
        // layer.open({
        //     content: "该手机号码有误",
        //     skin: 'msg',
        //     time: 3
        // });
    }
}
$('.recommend_phone').focus(function () {
    $(".divs").show()
    //失去焦点时执行的函数
})
function getToken(res) {
    sessionStorage.setItem("token",JSON.parse(res).token)
    token = sessionStorage.getItem("token")
}
// 支付动作
function payBtn() {
    if(!token){
        if(sessionStorage.getItem("broswer")=="android"){
            window['WebViewJsController']['jsGetData'](1, 1, null, getToken);
        }else{
            window.webkit.messageHandlers.token.postMessage({})
        } 
    }
    // console.log("phoneFlag1",phoneFlag1)
    // console.log("phoneFlag2",phoneFlag2)
    // 姓名
    var name = $(".name").val().trim();
    if (name == "") {
        layer.open({
            content: "请填写姓名",
            skin: 'msg',
            time: 3
        });
        return
    }
    if (name.length < 2 || name.length > 10) {
        layer.open({
            content: "姓名最少录入2个文字，最多录入10个文字",
            skin: 'msg',
            time: 3
        });
        return
    }
    // 手机号
    var phone = $(".phone").val().trim();
    var tel = /^1([38]\d|5[0-35-9]|7[3678])\d{8}$/;
    if (phone == "") {
        // alert(1)
        layer.open({
            content: "请填写手机号",
            skin: 'msg',
            time: 3
        });
        return
    }
    if (!(tel.test(phone))) {
        // alert(1)
        layer.open({
            content: "该手机号码有误",
            skin: 'msg',
            time: 3
        });
        return
    }
    // 收货所在地区
    var area = $(".area").val().trim();
    console.log(area)
    if (!area) {
        layer.open({
            content: "请选择收货所在地区",
            skin: 'msg',
            time: 3
        });
        return
    }
    // 收货详细地址
    var address = $(".address").val().trim();
    if (!address) {
        layer.open({
            content: "请填写收货详细地址",
            skin: 'msg',
            time: 3
        });
        return
    }
    // 推荐人姓名
    var recommend_name = $(".recommend_name").val().trim();
    if (recommend_name == "") {
        layer.open({
            content: "请填写推荐人姓名",
            skin: 'msg',
            time: 3
        });
        return
    }
    if (recommend_name.length < 2 || recommend_name.length > 10) {
        layer.open({
            content: "姓名最少录入2个文字，最多录入10个文字",
            skin: 'msg',
            time: 3
        });
        return
    }
    // 推荐人手机号
    var recommend_phone = $(".recommend_phone").val().trim();
    if (recommend_phone == "") {
        // alert(1)
        layer.open({
            content: "请填写推荐人手机号",
            skin: 'msg',
            time: 3
        });
        return
    }
    if (!(tel.test(recommend_phone))) {
        // alert(2)
        layer.open({
            content: "该推荐人手机号码有误",
            skin: 'msg',
            time: 3
        });
        return
    }
    if (name && phone && area && address && recommend_name && recommend_phone) {
        if (phoneFlag1 && phoneFlag2) {
            var data = {
                name,
                phone,
                area,
                address,
                recommend_name,
                recommend_phone
            }
            payFn(data)
        } else {
            layer.open({
                content: "该手机号码有误",
                skin: 'msg',
                time: 3
            });
        }
    } 
    // else {
    //     layer.open({
    //         content: "全为必填项，请将信息填写完整",
    //         skin: 'msg',
    //         time: 3
    //     });
    // }
}

// 支付提交
function payFn(e) {
    $.ajax({
        type: 'POST',
        url: httpUrl + 'api/act/yanka/submit',
        async: true,
        data: e,
        dataType: 'json',
        success: function (res) {
            console.log(res)
            if (res.status_code == '0') {
                if (sessionStorage.getItem("broswer") == 'mp') {
                    wx.miniProgram.navigateTo({
                        url: "/pages/package_mine/yanka_pay/main?mall_name=" + res.data.body + "&order_sn=" + res.data.order_sn + "&totalPrice=" + res.data.money + "&type=yanka"
                    });
                }
                if (sessionStorage.getItem("broswer") == 'android') {
                    var jsonData = { "order_type": "8", "order_price": res.data.money, "order_sn": res.data.order_sn, "body_title": res.data.body }
                    window.WebViewJsController.jsGetData(4, 35, JSON.stringify(jsonData), null)
                }
                if (sessionStorage.getItem("broswer") == 'ios') {
                    var obj = {
                        "order_type": "5",
                        "price": res.data.money,
                        "order_sn": res.data.order_sn,
                        "title": res.data.body
                    };
                    window.webkit.messageHandlers.pay.postMessage(obj)
                }
            } else {
                layer.open({
                    content: res.message,
                    skin: 'msg',
                    time: 2 //2秒后自动关闭
                });
            }
        },
        error: function () {
            layer.open({
                content: "服务器开了个小差...",
                skin: 'msg',
                time: 2 //2秒后自动关闭
            });
        }
    })
}
